package com.fruit.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * DataUtil
 * @author Administrator
 * @version 2017.12.01 11:40
 */
public class DataUtil {

	protected static final DecimalFormat NUMFORMAT = new DecimalFormat("#.00");
	
	/**
	 * getBigDecimal
	 * @param o o
	 * @return 返回BigDecimal
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static BigDecimal getBigDecimal(Object o) {
		try {
			if (o == null) {
				return new BigDecimal(0);
			}
			if (o instanceof BigDecimal) {
				return (BigDecimal) o;
			} else if (o instanceof Integer) {
				return new BigDecimal((Integer) o);
			} else if (o instanceof Double) {
				return new BigDecimal((Double) o);
			} else if (o instanceof Long) {
				return new BigDecimal((Long) o);
			} else if (o instanceof String) {
				return new BigDecimal((String) o);
			} else {
				return new BigDecimal(0);
			}
		} catch (Exception e) {
			return new BigDecimal(0);
		}
	}

	/**
	 * getBigDecimal
	 * @param o o
	 * @param scale scale
	 * @return 返回BigDecimal
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static BigDecimal getBigDecimal(Object o, int scale) {
		try {
			if (o == null) {
				return new BigDecimal(0).setScale(scale, BigDecimal.ROUND_DOWN);
			}
			if (o instanceof BigDecimal) {
				return ((BigDecimal) o).setScale(scale, BigDecimal.ROUND_DOWN);
			} else if (o instanceof Integer) {
				return new BigDecimal((Integer) o).setScale(scale, BigDecimal.ROUND_DOWN);
			} else if (o instanceof Double) {
				return new BigDecimal((Double) o).setScale(scale, BigDecimal.ROUND_DOWN);
			} else if (o instanceof Float) {
				return new BigDecimal((Float) o).setScale(scale, BigDecimal.ROUND_DOWN);
			} else if (o instanceof Long) {
				return new BigDecimal((Long) o).setScale(scale, BigDecimal.ROUND_DOWN);
			} else if (o instanceof String) {
				return new BigDecimal((String) o).setScale(scale, BigDecimal.ROUND_DOWN);
			} else {
				return new BigDecimal(0).setScale(scale, BigDecimal.ROUND_DOWN);
			}
		} catch (Exception e) {
			return new BigDecimal(0).setScale(scale, BigDecimal.ROUND_DOWN);
		}
	}

	/**
	 * mul
	 * @param obj obj
	 * @param scale scale
	 * @return 返回BigDecimal
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static BigDecimal mul(int scale, Object... obj) {
		BigDecimal result = BigDecimal.ONE;
		for (Object o : obj) {
			result = result.multiply(getBigDecimal(o));
		}
		return result.setScale(scale, BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * add
	 * @param obj obj
	 * @param scale scale
	 * @return 返回BigDecimal
	 * @author Administrator
	 * @version 2017.12.01 11:40
	 */
	public static BigDecimal add(int scale, Object... obj) {
		BigDecimal result = BigDecimal.ZERO;
		for (Object o : obj) {
			result = result.add(getBigDecimal(o));
		}
		return result.setScale(scale, BigDecimal.ROUND_HALF_UP);
	}

	
    public static String dealDouble(BigDecimal bigDecimal) {
        return dealDouble(bigDecimal.doubleValue());
    }
    
    public static String dealDouble(double count) {
        if (count < 0.005 && count > -0.005) {
            return "0.00";
        }
        String result = NUMFORMAT.format(count);
        return result.startsWith(".") ? "0" + result : result;
    }
}
